<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <!-- 头部 -->
        <el-form :model="page" ref="page" inline>
          <el-form-item label="关键字" label-width="80px" prop="keyword">
            <el-input
              v-model="page.keyword"
              size="small"
              placeholder="根据文章标题搜索"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="状态" label-width="80px" prop="state">
            <el-select
              v-model="page.state"
              size="small"
              placeholder="请选择"
              @change="SearchState"
            >
              <el-option
                v-for="item in status"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-button size="small" @click="clearClick">清除</el-button>
          <el-button type="primary" size="small" @click="search"
            >搜索</el-button
          >
          <el-button type="success" size="small" @click="addArticle">
            <i class="el-icon-edit"></i>
            新增技巧</el-button
          >
        </el-form>
        <el-alert type="info" show-icon style="margin-bottom: 15px">
          <template slot="title">数据一共{{ page.total }}条</template>
           </el-alert
        >
        <!-- 表格部分 -->
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="index" label="序号" width="100">
          </el-table-column>
          <el-table-column prop="title" label="文章标题" width="350">
            <template slot-scope="{ row }"
              >{{ row.title
              }}<a
                :href="row.videoURL"
                class="el-icon-film"
                v-if="row.videoURL"
              ></a
            ></template>
          </el-table-column>
          <el-table-column prop="visits" label="阅读数"> </el-table-column>
          <el-table-column prop="username" label="录入人"> </el-table-column>
          <el-table-column prop="createTime" label="录入时间" width="150">
            <template slot-scope="{ row }">{{
              row.createTime | parseTimeByString
            }}</template>
          </el-table-column>
          <el-table-column prop="state" label="状态" width="50">
            <template slot-scope="{ row }">{{
              row.state | questionStatus
            }}</template>
          </el-table-column>
          <el-table-column label="操作" width="200">
            <template slot-scope="{ row }">
              <el-button type="text" size="medium" @click="preview(row)">
                预览</el-button
              >
              <el-button type="text" size="medium" @click="changeState(row)">
                {{ !row.state | questionStatus }}</el-button
              >
              <el-button
                type="text"
                size="medium"
                :disabled="row.state == 0"
                @click.native.prevent="edit(row)"
              >
                修改
              </el-button>
              <el-button
                type="text"
                size="medium"
                :disabled="row.state == 0"
                @click="delArticle(row)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          background
          style="margin-top: 20px; text-align: right"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.page"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="page.pagesize"
          layout="prev, pager, next, sizes, jumper"
          :total="page.total"
        >
        </el-pagination>
        <!-- 新增文章弹层 -->
        <addArticle
          :forms="editObj"
          :addDialogVisible.sync="addDialogVisible"
          @updateArticle="getArticleList"
        />
        <!-- 预览弹层 -->
        <el-dialog
          title="预览文章"
          :visible.sync="preDialog"
          width="800px"
          @close="preCancel"
        >
          <h2>{{ preObj.title }}</h2>
          <div style="margin-bottom: 10px">
            <span class="pre">{{ preObj.createTime | parseTimeByString }}</span>
            <span class="pre">{{ preObj.username }}</span>
            <span class="el-icon-view pre"></span>
            <span class="pre">{{ preObj.visits }}</span>
          </div>
          <div class="content">
            <span v-html="preObj.articleBody"></span>
          </div>
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>
<script>
import * as articleApi from '@/api/hmmm/articles.js'
import { status } from '@/api/hmmm/constants.js'
import addArticle from '../components/articleAdd.vue'
export default {
  components: {
    addArticle
  },
  data () {
    return {
      status,
      page: {
        page: 1,
        pagesize: 5,
        total: 0,
        keyword: '',
        id: ''
      },
      tableData: [],
      changeText: '',
      addDialogVisible: false,
      preDialog: false,
      preObj: {},
      editObj: {}
    }
  },
  created () {
    this.getArticleList()
  },
  methods: {
    SearchState (value) {
      console.log(11);
      console.log(value);
      this.page.state = value
      console.log(this.page);
    },
    async getArticleList () {
      const { data } = await articleApi.list(this.page)
      this.tableData = data.items
      this.page.total = data.counts
    },
    clearClick () {
      this.page = {}
      this.page.pagesize = 10
      this.page.page = 1
      this.getArticleList()
    },
    async search () {
      if (!this.page.keyword && this.page.state === "") {
        return this.$message.warning('请输入文章标题或状态')
      }
      this.page.page = 1
      const { data } = await articleApi.list(this.page)
      console.log(data);
      this.tableData = data.items
      this.page.total = data.counts
      console.log(this.page)
    },
    addArticle () {
      this.addDialogVisible = true
    },
    async preview (row) {
      this.preDialog = true
      const { data } = await articleApi.detail(row)
      this.preObj = data
      this.preObj.username = row.username
    },
    preCancel () {
      this.preDialog = false
    },
    async changeState (row) {
      if (row.state == 1) {
        row.state = 0
        this.changeText = '启用'
      } else if (row.state == 0) {
        row.state = 1
        this.changeText = '禁用'
      }
      await articleApi.changeState(row)
      this.$message.success('操作成功')
      this.getArticleList()
    },
    delArticle (row) {
      this.$confirm('此操作将永久删除改文章，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await articleApi.remove(row)
        this.getArticleList()
        this.$message.success('操作成功')
      }).catch(err => err)
    },
    async edit (row) {
      const { data } = await articleApi.detail(row)
      this.editObj = data
      console.log(this.editObj)
      this.addArticle()
    },
    handleSizeChange (num) {
      this.page.pagesize = num
      this.getArticleList()
    },
    async handleCurrentChange (num) {
      this.page.page = num
      // this.getArticleList()
      const { data } = await articleApi.list(this.page)
      console.log(data);
      this.page.pagesize = Number(data.pagesize)
      this.tableData = data.items
      this.page.total = data.counts
      console.log(this.page);
    },
  }
}
</script>

<style scoped lang='scss'>
::v-deep .el-form {
  position: relative;
}
::v-deep .el-button--success {
  position: absolute;
  top: 0;
  right: 0;
}
.el-icon-film {
  color: #00f;
  font-size: 18px;
}
.pre {
  margin: 0 5px;
}
.content {
  background: #f5f5f5;
  padding: 10px;
  border: 1px dashed #cccccc;
}
</style>
